package com.example.smartlearning.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.smartlearning.dao.LessonDao;
import com.example.smartlearning.entity.LessonEntity;
import com.example.smartlearning.entity.ResultEntity;
import com.example.smartlearning.service.LessonService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class LessonServiceImpl extends ServiceImpl<LessonDao, LessonEntity> implements LessonService {
    @Resource
    LessonDao lessonDao;

    private static final Logger logger = LoggerFactory.getLogger(LessonServiceImpl.class);//日志。getLogger需要传入当前的类名


    public String getAllLesson() {
        try {
            return ResultEntity.success(lessonDao.selectList(null));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 获取所有课程列表
     *
     * @param start 开始位置
     * @param limit 数据条数
     * @return 返回
     */
    @Override
    public String getList(int start, int limit) {
        try {
            if (start > Integer.parseInt(lessonDao.Count())) {
                return ResultEntity.LessonFail(-10001);
            }
            return ResultEntity.success(lessonDao.selectAll(start, limit));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 用户点赞接口
     *
     * @param lessonId 课程id
     * @param userId   用户id
     * @return 返回
     */
    @Override
    public String likeCourse(int lessonId, int userId) {
        try {
//            lessonDao.addUpvote(lessonId, userId);
            return ResultEntity.success(lessonDao.addUpvote(lessonId, userId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }

    }

    /**
     * 用户取消点赞接口
     *
     * @param lessonId 课程id
     * @param userId   用户id
     * @return 返回
     */
    @Override
    public String dislikeCourse(int lessonId, int userId) {
        try {
//            lessonDao.addUpvote(lessonId, userId);
            return ResultEntity.success(lessonDao.cancelUpvote(lessonId, userId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 根据课程id查询课程详情
     *
     * @param lessonId 课程id
     * @return 返回
     */
    @Override
    public String selectDetail(int lessonId) {
        try {
            return ResultEntity.success(lessonDao.selectLessonDetail(lessonId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 根据课程id获取课程评价信息
     *
     * @param lessonId 课程id
     * @return 返回
     */
    @Override
    public String selectRemarkByLesson(int lessonId) {
        try {
            return ResultEntity.success(lessonDao.selectRemarkByLesson(lessonId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 课程评价点赞接口
     *
     * @param lessonRemarkId 课程评价id
     * @return 返回
     */

    @Override
    public String agreeRemark(int lessonRemarkId) {
        try {
            return ResultEntity.success(lessonDao.agreeRemark(lessonRemarkId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * @param lessonId           课程id
     * @param lessonRemarkText   评价内容
     * @param lessonRemarkUserId 用户id
     * @return 返回
     */
    @Override
    public String appearRemark(int lessonId, String lessonRemarkText, int lessonRemarkUserId) {
        try {
            return ResultEntity.success(lessonDao.appearRemark(lessonId, lessonRemarkText, lessonRemarkUserId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 增加课程
     *
     * @param lessonLink    链接
     * @param lessonTitle   标题
     * @param lessonTeacher 教师
     * @param lessonPrice   价格
     * @return 返回
     */
    public String addLesson(String lessonLink, String lessonTitle, String lessonTeacher, String lessonPrice) {
        LessonEntity lessonEntity = new LessonEntity();
        lessonEntity.setLessonLink(lessonLink);
        lessonEntity.setLessonPrice(lessonPrice);
        lessonEntity.setLessonTeacher(lessonTeacher);
        lessonEntity.setLessonUpvote(0);
        lessonEntity.setLessonTitle(lessonTitle);
        try {
            return ResultEntity.success(lessonDao.insert(lessonEntity));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 删除课程方法
     * @param lessonId 课程id
     * @return 返回
     */
    public String deleteLesson(int lessonId) {
        try {
            return ResultEntity.success(lessonDao.deleteById(lessonId));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }
    /**
     * 更新课程方法
     * @param lessonId 课程id
     * @return 返回
     */
    public String updateLesson(int lessonId,String lessonLink, String lessonTitle, int lessonUpvote,String lessonTeacher, String lessonPrice) {
        LessonEntity lessonEntity = new LessonEntity();
        lessonEntity.setLessonId(lessonId);
        lessonEntity.setLessonLink(lessonLink);
        lessonEntity.setLessonPrice(lessonPrice);
        lessonEntity.setLessonTeacher(lessonTeacher);
        lessonEntity.setLessonUpvote(lessonUpvote);
        lessonEntity.setLessonTitle(lessonTitle);

        try {
            return ResultEntity.success(lessonDao.updateById(lessonEntity));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }


}
